import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TokenizerAnalyzerUtils {
    public static final String ENCODING = "UTF-8";

    public TokenizerAnalyzerUtils() {
    }

    public static String getAnalyzerResult(String input) {
        StringReader sr = new StringReader(input);
        IKSegmenter ik = new IKSegmenter(sr, true); // true is use smart
        Lexeme lex = null;
        List<String> stopWordsList = getStopWordsList();
        StringBuilder stringBuilder = new StringBuilder();

        try {
            while ((lex = ik.next()) != null) {
                if (stopWordsList != null && stopWordsList.contains(lex.getLexemeText())) {
                    continue;
                }
                stringBuilder.append(lex.getLexemeText() + " ");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("failed to parse input content");
        }
        return stringBuilder.toString();
    }

    private static List<String> getStopWordsList() {
        try {
            // 使用 getResourceAsStream 代替获取文件路径
            InputStream inputStream = TokenizerAnalyzerUtils.class.getResourceAsStream("/library/stopwords.dic");
            if (inputStream == null) {
                throw new IOException("Cannot find stopwords.dic in /library/ directory");
            }

            // 使用缓冲流读取内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, ENCODING));
            List<String> stopWordList = new ArrayList<>();
            String line;
            while ((line = reader.readLine()) != null) {
                stopWordList.add(line.trim());
            }
            return stopWordList;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Failed to load stop word dictionary: " + e.getMessage());
            return new ArrayList<>(); // 返回空列表避免NPE
        }
    }

    // Simple constant class to avoid dependency
    static class Constants {
        public static final String BLANKSPACE = " ";
        public static final String ENCODING = "UTF-8";
    }
}